package nuc.soft.xsf.controller;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import net.sf.json.JSONObject;
import nuc.soft.xsf.model.Dictionary;

import nuc.soft.xsf.service.IDictionaryService;

import nuc.soft.xsf.tools.Tools;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/DictionaryController")
public class DictionaryController {
    @Resource
    private IDictionaryService dictionaryService;
    
    @RequestMapping(value="/add.action" ,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String addDictionary(Dictionary dictionary){
    	JSONObject json=new JSONObject();
    	dictionary.setDictionaryDeleteflag(0);
    	dictionaryService.insert(dictionary);
    	json.put("data", dictionary);
    	return json.toString();
    }
    
    @RequestMapping(value="/update.action" ,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String updateDictionary(Dictionary dictionary){
    	JSONObject json=new JSONObject();
    	dictionaryService.update(dictionary);
    	json.put("data",dictionary);
    	return json.toString();
    }
    
    @RequestMapping(value="/delete.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String deleteUser(String ids){
    	String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			Dictionary dictionary = dictionaryService.findById(Integer.parseInt(strTmp));
			if (dictionary != null) {
				dictionary.setDictionaryDeleteflag(1);
				dictionaryService.update(dictionary);
				success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}
    	
    @RequestMapping(value="/findByCondition.action",produces="text/html;charset=utf-8")
    @ResponseBody
    public String findByCondition(Dictionary dictionary){
    	JSONObject json=new JSONObject();
    	Map map=Tools.transBean2Map(dictionary);
    	List<Dictionary> dictionaryList=dictionaryService.findByCondition(map);
        json.put("dictionaryList", dictionaryList);
    	return json.getString("dictionaryList");
    	
    }
   
	
}
